/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import ui.ScreenPainter;
import xmlutils.MapXmlData;

/**
 *
 * @author Administrator
 */
public class Engine extends GameCanvas {

    private static Engine engine = new Engine();
    private ScreenPainter screenPainter;
    private Image[] images;
    private boolean pause = false;
    //String url = "http://www.google.com.eg/images/nav_logo3.png";
    String url = "http://upload.wikimedia.org/wikipedia/en/thumb/c/c2/Android-logo.svg/500px-Android-logo.svg.png";

    public Engine() {
        super(false);
        init();
    }

    public static Engine getInstance() {
        return engine;
    }

    public void init() {
        try {
//            images[0] = Image.createImage("/resources/1.png");
//            images[1] = Image.createImage("/resources/2.png");
//            images[2] = Image.createImage("/resources/3.png");
//            images[3] = Image.createImage("/resources/4.png");
//            images[4] = Image.createImage("/resources/5.png");

            MapXmlData mapData = new MapXmlData("http://www.alertmepro.com/images/format.xml");
            ImagesFactory imagesFactory = ImagesFactory.getInstance(mapData, 2);
//
//
//            RadarData radarData = mapData.getRadarData();
//            ZoomLevelData zoomLevel2 = radarData.getZoomLevelAt(1);
//
//            LoopData loopData;
//            for (int i = 0; i < zoomLevel2.getLoopsSize(); i++) {
//                loopData = zoomLevel2.getLoopAt(i);
//                images[i] = ImageLoader.getImageFromUrl(loopData.getUrl());
//            }
            images = imagesFactory.loadRadarImages(2);
            screenPainter = new ScreenPainter(this.getWidth(), this.getHeight());
            loopImages();

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void loopImages() {
        Thread t = new Thread() {

            public void run() {
                try {
                    while (!pause) {
                        for (int i = 0; i < images.length && !pause; i++) {
                            screenPainter.Paint(getGraphics(), images[i], 0);
                            repaint();
                            Thread.sleep(500);
                            if (i == 4 && !pause) {
                                Thread.sleep(1000);
                            }
                        }
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        t.start();
    }

    public void keyRepeated(int keyCode) {
        keyPressed(keyCode);
    }

    public void keyPressed(int keyCode) {
        try {
            System.out.println("key code " + keyCode);
            if (keyCode == -7 || keyCode == -6 || keyCode == 35 || keyCode == 42) {
                if (this.pause == true) {

                    this.pause = false;
                    loopImages();
                }
            } else {

                this.pause = true;
                screenPainter.Paint(getGraphics(), images[4], keyCode);
                this.repaint();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        super.keyPressed(keyCode);
    }




}
